package cn.hyy.FVOnline.service.impl;

import cn.hyy.FVOnline.entity.Evaluate;
import cn.hyy.FVOnline.entity.Goods;
import cn.hyy.FVOnline.entity.PageEntitys;
import cn.hyy.FVOnline.entity.Shop;
import cn.hyy.FVOnline.mapper.EvaluateMapper;
import cn.hyy.FVOnline.service.GoodsService;
import cn.hyy.FVOnline.service.ShopService;
import cn.hyy.FVOnline.mapper.ShopMapper;
import cn.hyy.FVOnline.utils.Constant;
import cn.hyy.FVOnline.entity.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hyy.FVOnline.mapper.GoodsMapper;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageEntitys findPageGoods(Integer shopid, Integer page, Integer pageSzie) {
        Integer start = (page - 1) * pageSzie;
        List<Goods> pageGoods = goodsMapper.findPageGoods(shopid, start, pageSzie);
        PageEntitys pageEntity = new PageEntitys();
        pageEntity.setTotal(goodsMapper.getTotal(shopid));
        pageEntity.setCurrent(page);
        pageEntity.setGoods(pageGoods);
        return pageEntity;
    }

    @Override
    public PageEntitys findByTypeName(Integer shopid, Integer page, Integer pageSzie, String type, String goodsname) {

        Integer start = (page - 1) * pageSzie;

        goodsname = "%" + goodsname + "%";

        List<Goods> pageGoods = goodsMapper.findByTypeName(shopid, start, pageSzie,type,goodsname);

        PageEntitys pageEntity = new PageEntitys();

        pageEntity.setTotal(goodsMapper.getTotalByTypeName(shopid,type,goodsname));
        pageEntity.setCurrent(page);
        pageEntity.setGoods(pageGoods);

        return pageEntity;
    }

    @Override
    public void deleteGoods(Integer id) {
        goodsMapper.deleteGoods(id);
    }

    @Override
    public void updateGoods(Goods goods) {
        goodsMapper.updateGoods(goods);
    }

    @Override
    public void newGoods(Goods goods, MultipartFile file) {
        InputStream is = null;
        FileOutputStream os =null;
        try {
            goodsMapper.newGoods(goods);
            File hs = new File(Constant.goodsPath + goods.getId() + ".png");
            if (!hs.exists()){
                hs.createNewFile();
            }
            is = file.getInputStream();
            os = new FileOutputStream(hs);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) > 0) {
                os.write(buffer, 0, len);
            }
            goods.setHsPath(Constant.goodsPath + goods.getId() + ".png");
            goodsMapper.setPath(goods);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (is != null){
                    is.close();
                }
                if (os!=null){
                    os.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public BufferedImage findImg(String goodsId) {
        InputStream is = null;
        BufferedImage bi = null;

        try {


            is = new FileInputStream(Constant.goodsPath + goodsId + ".png");

            bi = ImageIO.read(is);

        }catch (Exception e){
        }finally {
            try {
                if (is != null){
                    is.close();
                }
            }catch (Exception e){
            }
        }

        return bi;
    }


    @Override
    public PageEntitys findByName(Integer shopid, Integer page, Integer pageSzie, String goodsname) {
        Integer start = (page - 1) * pageSzie;

        goodsname = "%" + goodsname + "%";

        List<Goods> pageGoods = goodsMapper.findByName(shopid, start, pageSzie,goodsname);

        PageEntitys pageEntity = new PageEntitys();

        pageEntity.setTotal(goodsMapper.getTotalByName(shopid,goodsname));
        pageEntity.setCurrent(page);
        pageEntity.setGoods(pageGoods);

        return pageEntity;
    }

    @Autowired
    private ShopService shopService;

    @Override
    public PageEntitys findGoodsList(Integer page, Integer pageSzie, String goodsname) {
        Integer start = (page - 1) * pageSzie;
        goodsname = "%" + goodsname + "%";
        List<Goods> pageGoods = goodsMapper.findGoodsList(start, pageSzie,goodsname);
        for (Goods goods: pageGoods) {
            Shop shop = shopService.findById(goods.getShopid());
            goods.setShop(shop);
        }
        PageEntitys pageEntity = new PageEntitys();
        pageEntity.setTotal(goodsMapper.getListTotal(goodsname));
        pageEntity.setCurrent(page);
        pageEntity.setGoods(pageGoods);
        return pageEntity;
    }

    @Autowired
    private EvaluateMapper evaluateMapper;

    @Override
    public Goods findById(Integer goodsid) {
        Goods goods = goodsMapper.findById(goodsid);
        List<Evaluate> evaluates = evaluateMapper.findByGid(goodsid);
        goods.setEvaluates(evaluates);
        return goods;
    }



    @Override
    public PageEntitys getCar(List<String> lists) {
        PageEntitys pageEntitys = new PageEntitys();
        List<Car> cars = new ArrayList<>();
        for (String s : lists) {
            String id = s.split("_")[0];
            String number = s.split("_")[1];
            Goods goods = goodsMapper.findById(Integer.parseInt(id));
            Car car = new Car();
            car.setGoods(goods);
            car.setNumber(Integer.parseInt(number));
            cars.add(car);
        }
        pageEntitys.setCars(cars);
        return pageEntitys;
    }

    @Override
    public void delGoods(Integer id) {
        goodsMapper.delGoods(id);
    }
    @Autowired
    private ShopMapper shopMapper;
    @Override
    public PageEntitys findGoodsList2(Integer page, Integer pageSzie, String goodsname) {
        goodsname = "%" + goodsname + "%";
        Integer start = (page - 1) * pageSzie;
        List<Goods> pageGoods = goodsMapper.findGoodsList2(start, pageSzie,goodsname);
        for (Goods goods: pageGoods) {
            Shop shop = shopService.findById(goods.getShopid());
            goods.setShop(shop);
        }
        PageEntitys pageEntity = new PageEntitys();
        pageEntity.setTotal(goodsMapper.getListTotal2(goodsname));
        pageEntity.setCurrent(page);
        pageEntity.setGoods(pageGoods);
        return pageEntity;
    }

    @Override
    public void addGoods(Integer id) {
        goodsMapper.addGoods(id);
        Goods goods = goodsMapper.findById(id);
        shopMapper.addGoods(goods.getShopid());
    }
}
